﻿using System;
using System.Windows.Forms;
using TestDVR.Net;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Net;
using System.Collections.Generic;
namespace TestDVR
{
    public partial class Form1 : Form
    {
        int updateNum = 0;
        int getipbymacNum = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private FileInfo[] getFileName()
        {
            string exePath = Application.StartupPath+"\\App_Data";
            DirectoryInfo theFolder = new DirectoryInfo(exePath);
            FileInfo[] fileInfo=theFolder.GetFiles("*.log");
            return fileInfo;
        }

        public int InsertUpdateTable(string mac,string ip)
        {
            DbHelper db = new DbHelper();
            string strSql = "insert into update1(mac,ip) values('" +mac+"','"+ip+"')";
            var cmd=db.GetSqlStringCommond(strSql);
            return db.ExecuteNonQuery(cmd);
        }

        public int GetUpdateByMac(string mac)
        {
            DbHelper db = new DbHelper();
            string strSql = "select *from update1 where mac='" + mac + "'";
            var cmd = db.GetSqlStringCommond(strSql);
            return Convert.ToInt32(db.ExecuteScalar(cmd));
        }

        public int InsertGetipbymacTable(string mac, string ip)
        {
            DbHelper db = new DbHelper();
            string strSql = "insert into getipbymac(mac,ip) values('" + mac + "','" + ip + "')";
            var cmd = db.GetSqlStringCommond(strSql);
            return db.ExecuteNonQuery(cmd);
        }

        public int GetgetsilentByMac(string mac)
        {
            DbHelper db = new DbHelper();
            string strSql = "select *from getipbymac where mac='" + mac + "'";
            var cmd = db.GetSqlStringCommond(strSql);
            return Convert.ToInt32(db.ExecuteScalar(cmd));
        }

        public List<string> GetMac()
        {
            DbHelper db = new DbHelper();
            string strSql = "select mac from update1";
            var cmd = db.GetSqlStringCommond(strSql) as OleDbCommand;
            OleDbDataReader reader = db.ExecuteReader(cmd) as OleDbDataReader;
            List<string> list=new List<string>();
            while (reader.Read())
            {
                list.Add(reader.GetString(0));
            }
            return list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileInfo[] fileInfo=getFileName();
            foreach (FileInfo files in fileInfo)
            {
                string fileName = files.Directory + "\\" + files.Name;
                StreamReader sr = new StreamReader(fileName);
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    if (str.Contains("update"))
                    {
                        updateNum++;
                        string[] val = str.Split(' ');
                        foreach (string s in val)
                        {
                            if (s.Contains("value"))
                            {
                                if (GetUpdateByMac(s.Split('=')[1])==0)
                                {
                                    InsertUpdateTable(s.Split('=')[1], "211.154.153.98");
                                }
                            }
                        }
                        txtDnsNums.Text = updateNum.ToString();
                    }
                    if (str.Contains("getipbymac"))
                    {
                        getipbymacNum++;
                        string[] val = str.Split(' ');
                        foreach (string s in val)
                        {
                            if (s.Contains("value"))
                            {
                                if (GetgetsilentByMac(s.Split('=')[2]) == 0)
                                {
                                    InsertGetipbymacTable(s.Split('=')[2], "211.154.153.98");
                                }
                            }
                        }
                        txtHttpNum.Text = getipbymacNum.ToString();
                    }
                }
            }

              //IPHostEntry entry=System.Net.Dns.GetHostEntry("211.154.153.98");   
            MyDnS dns = new MyDnS();
            /*dns.ServerName = "42.96.152.130";
            dns.userName = "Administrator";//
            dns.passWord = "a62411570";*/
            dns.CreateZone("yaosansi.com");
            dns.CreateAType("yaosansi.com", "www", "2.2.2.2", "3600");
            MessageBox.Show(dns.ErrMessage);
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            string domain=txtDomain.Text;
            string val=txtValue.Text;
            if(domain!=""&&val!="")
            {
                string url="http://www."+domain+"/update.php?value="+val;
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                req.UserAgent = "lhltest";
                //req.Headers.Add("User-Agent", "Test Program");
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream);
                string ret = sr.ReadToEnd();
                response.Close();
                string dns = "";
                txtHttpNum.Text = "1";
                if (ret.Contains("SUCC"))
                {
                    dns=ret.Split(':')[3]+"\r\n";
                    txtDns.Text =txtDns.Text+dns;
                    txtDnsNums.Text = "1";
                }
            }
            else
            {
                MessageBox.Show("域名和http请求值不能为空");
            }
            
        }

        private void btnUpdates_Click(object sender, EventArgs e)
        {
            if (txtDomain.Text != "")
            {
                List<string> list = GetMac();
                int httpNum = 0;
                int dnsSuccNum = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    txtValue.Text = list[i];
                    string url = "http://www." + txtDomain.Text + "/update.php?value=" + list[i];
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.UserAgent = "lhltest";
                    //req.Headers.Add("User-Agent", "Test Program");
                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(stream);
                    string ret = sr.ReadToEnd();
                    response.Close();
                    httpNum++;
                    string dns = "";
                    txtHttpNum.Text = httpNum.ToString();
                    if (ret.Contains("SUCC"))
                    {
                        dnsSuccNum++;
                        dns = ret.Split(':')[3] + "\r\n";
                        txtDns.Text = txtDns.Text + dns;
                        txtDnsNums.Text = dnsSuccNum.ToString();
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入域名信息");
            }
        }
    }
}
